package com.wind.taobao.api.huabao.convert
{
	import com.wind.taobao.api.huabao.model.PosterChannel;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * PosterChannel 转换器
	 * @author fxy
	 *
	 */
	public class PosterChannelConvert
	{
		/**
		 * 转换PosterChannel集合
		 */
		public static function convertPosterChannels(value:Object, type:String="json"):ArrayCollection
		{
			var posterChannels:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							posterChannels.addItem(convertPosterChannel((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return posterChannels;
		}

		/**
		 * 转换PosterChannel
		 */
		public static function convertPosterChannel(value:Object, type:String="json"):PosterChannel
		{
			var posterChannel:PosterChannel=new PosterChannel();
			if (value == null)
				return posterChannel;
			if (type == TaobaoConstants.XML)
			{
				posterChannel=convertXMLToPosterChannel(XML(value), posterChannel);
			}
			else
			{
				posterChannel=convertJSONToPosterChannel(value, posterChannel);
			}
			return posterChannel;
		}

		/**
		 * JSON转换为PosterChannel
		 */
		private static function convertJSONToPosterChannel(json:Object, posterChannel:PosterChannel):PosterChannel
		{
			if (json.hasOwnProperty(TaobaoConstants.ID))
			{
				posterChannel.id=json[TaobaoConstants.ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.NAME))
			{
				posterChannel.name=json[TaobaoConstants.NAME];
			}
			if (json.hasOwnProperty(TaobaoConstants.CN_NAME))
			{
				posterChannel.cn_name=json[TaobaoConstants.CN_NAME];
			}
			if (json.hasOwnProperty(TaobaoConstants.DESC))
			{
				posterChannel.desc=json[TaobaoConstants.DESC];
			}
			if (json.hasOwnProperty(TaobaoConstants.URL))
			{
				posterChannel.url=json[TaobaoConstants.URL];
			}
			return posterChannel;
		}

		/**
		 * XML转换为PosterChannel
		 */
		private static function convertXMLToPosterChannel(value:XML, posterChannel:PosterChannel):PosterChannel
		{
			return posterChannel;
		}

	}
}